import { Controller } from "../../mvc/core/Controller";
import { IKingWarAd } from "../../../modules/interfaces/IKingWarAd";
import { KingWarAdModule } from "../../../modules/KingWarAdModule";
import { KingWarHintBtn } from "../view/kingWarAd/KingWarHintBtn";
import { KingWarRuleBaseWindow } from "../view/kingWarAd/KingWarRuleBaseWindow";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { DataEvent } from "../events/DataEvent";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { Cache } from "../cache/Cache";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { ClockManager } from "../manager/ClockManager";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SArenaCrossRecords } from "../../../Message/Public/SArenaCrossRecords";
import { SArenaCrossInfo } from "../../../Message/Public/SArenaCrossInfo";
type int = number;
//class KingWarController
    
    export  class KingWarController extends Controller
    {
       

        public get kingwarModel(): IKingWarAd
        {
            if (null == this._kingwarModel) 
            {
                this._kingwarModel = new KingWarAdModule();
            }
            return this._kingwarModel;
        }

        public get kingWarHintBtn(): KingWarHintBtn
        {
            if (!this._kingWarHintBtn) 
            {
                this._kingWarHintBtn = new KingWarHintBtn();
            }
            return this._kingWarHintBtn;
        }

        public get ruleWindow(): KingWarRuleBaseWindow
        {
            if (null == this._ruleWindow) 
            {
                this._ruleWindow = new KingWarRuleBaseWindow();
            }
            return this._ruleWindow;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.KingWarSkipTab, this.onSkipToTabBar);
            Dispatcher.addEventListener(EventName.ShowKingWarModule, this.onShowKingWarModule);
            Dispatcher.addEventListener(EventName.KingWarGetToplist, this.onKingWarGetToplistHandler);
            Dispatcher.addEventListener(EventName.KingWarGetNote, this.onGetKingWarNote);
            Dispatcher.addEventListener(EventName.KingWarSignUp, this.goSignUp);
            Dispatcher.addEventListener(EventName.KingWarRuleDescToTabBar, this.onToKWTabBar);
            NetDispatcher.addCmdListener(ServerCommand.ArenaCrossTopListKingWarUpdate, this.onArenaCrossToplistHandler);
            NetDispatcher.addCmdListener(ServerCommand.ArenaCrossInfoCommand, this.onArenaCrossInfoCommand);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicArenaCrossRecords, this.onReachDateNote);
            return;
        }

        /* internal  */onToKWTabBar(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this.ruleWindow.isHide) 
            {
                this.ruleWindow.show();
                this.ruleWindow.showToTab(loc1);
            }
            else 
            {
                this.ruleWindow.hide();
            }
            return;
        }

        /* internal  */onLevelChange(arg1: Object=null): void
        {
            this.showHintBtn();
            return;
        }

        /* internal  */onShowAutoSignTips(arg1: DataEvent): void
        {
            Alert.showCloseBtn = true;
            Alert.okLabel = Language.getString(70432);
            Alert.calcelLabel = Language.getString(70433);
            Alert.buttonWidth = 75;
            Alert.show(Language.getString(70430), null, Alert.OK | Alert.CANCEL, null, this.onAlertClickHandler);
            return;
        }

        /* internal  */onAlertClickHandler(arg1: int): void
        {
            var loc1=Cache.instance.scene.getNpcByEffect(ENpcEffect._ENpcEffectTXDYCheckIn);
            if (arg1 != Alert.OK) 
            {
                AIManager.onAIControl(loc1, AIType.AI_Convey);
            }
            else 
            {
                AIManager.onAutoPathAIControl(loc1);
            }
            return;
        }

        /* internal  */showHintBtn(): void
        {
            var loc1=null;
            var loc2=NaN;
            if (cache.kingwar.sArenaInfo) 
            {
                loc1 = cache.kingwar.sArenaInfo;
                loc2 = ClockManager.instance.nowDate.getTime();
                if (loc1.processDt[0] && loc1.processDt[0].getTime() <= loc2 && loc2 <= loc1.processDt[1].getTime()) 
                {
                    if (cache.role.entityInfo.level >= 60 && cache.kingwar.sArenaInfo.myProcess == -1) 
                    {
                        this.kingWarHintBtn.show();
                    }
                    else 
                    {
                        this.kingWarHintBtn.hide();
                    }
                }
                else 
                {
                    this.kingWarHintBtn.hide();
                }
            }
            return;
        }

        /* internal  */goSignUp(arg1: DataEvent): void
        {
            GameProxy.copy.registArenaCorss();
            return;
        }

        /* internal  */onGetKingWarNote(arg1: DataEvent): void
        {
            GameProxy.copy.getArenaCorssRecords(arg1.data as Date);
            return;
        }

        /* internal  */onReachDateNote(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SArenaCrossRecords;
            cache.kingwar.sArenaNote = loc1;
            var loc2=cache.kingwar.sArenaInfo;
            this.kingwarModel.updateInfo();
            return;
        }

        /* internal  */onArenaCrossInfoCommand(arg1: SArenaCrossInfo): void
        {
            cache.kingwar.sArenaInfo = arg1;
            this.kingwarModel.updateInfo();
            return;
        }

        /* internal  */onShowKingWarModule(arg1: DataEvent): void
        {
            this.showKingWarModule();
            return;
        }

        /* internal  */showKingWarModule(): void
        {
            var loc1=cache.kingwar.sArenaInfo;
            if (this.kingwarModel.isHide) 
            {
                this.kingwarModel.show();
                GameProxy.copy.getArenaCrossInfo();
            }
            else 
            {
                this.kingwarModel.hide();
            }
            return;
        }

        /* internal  */onSkipToTabBar(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.kingwarModel.show();
            this.kingwarModel.showToTab(loc1);
            return;
        }

        /* internal  */onKingWarGetToplistHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.copy.getArenaCorssToplist(loc1.process, loc1.onlyMyServer, false, loc1.page, loc1.pageSize);
            return;
        }

        /* internal  */onArenaCrossToplistHandler(arg1: Object): void
        {
            if (this._kingwarModel && !this._kingwarModel.isHide) 
            {
                this._kingwarModel.updateArenaCorssToplist();
            }
            return;
        }

        private /* var */_kingwarModel: IKingWarAd;

        private /* var */_kingWarHintBtn: KingWarHintBtn;

        private /* var */_ruleWindow: KingWarRuleBaseWindow;
    }
